<?php
	$tplCate = new XTemplate('view/category/list.htm');
	$parentId 		= _POST('parent_id');
	$categoryName 	= _POST('category_name');
	$error 			= '';
	$modeForm		= _POST('modeform');
	$id				= _POST('id');
	if($_POST){
		if($categoryName == ''){
			$error .= '<li>Bạn chưa nhập tên danh mục.</li>';
		}
		if($error == ''){
			$data = array();
			$data['parent_id'] 		= ($parentId !=- 1) ? $parentId : 0;
			$data['category_name'] 	= $categoryName;
			if($modeForm==1){
				$db->update('tblcategory', $data, "category_id=$id");
			}else{
				$db->insert('tblcategory', $data);
			}
		}else{
			$tplCate->assign('error', $error);
		}	
	}
	$sql = 'SELECT * FROM tblcategory WHERE 1=1';
	$categories = $db->fetchAll($sql);
	
	$tree = creatTree($categories,'site/category/getInfoCategory.php');
	$sltCategory = getCombo('tblcategory', 'category_id', 'category_name', 'parent_id', 'Danh mục gốc', null, null, null, 'input-text-medium');
	
	
	$tplCate->assign('tree', $tree);
	$tplCate->assign('sltCategory', $sltCategory);
	$tplCate->parse('list');
	$content = $tplCate->text('list');
?>